package com.baiqian.doupo.gui;

import com.baiqian.doupo.DouPo;
import com.baiqian.doupo.Utils;
import com.baiqian.doupo.capability.DouPoModCapability;
import com.baiqian.doupo.capability.douqi.IDouQiCapability;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.common.util.LazyOptional;

public class PlayerRattributeGui extends Screen {
    private static final ResourceLocation PlayerRattributeGuiBgImg = new ResourceLocation(Utils.MOD_ID, "textures/gui/player_attribute_gui.png");
    private TranslationTextComponent content;
    private String soul_property_str = "";
    private String body_str;
    private String eye_str;
    private boolean[] soul_property;
    private int fireNum = 0;
    private LazyOptional<IDouQiCapability> douqiCap = null;

    public PlayerRattributeGui(ITextComponent titleIn) {
        super(titleIn);
        PlayerEntity player = Minecraft.getInstance().player;
        if (player != null) {
            douqiCap = player.getCapability(DouPoModCapability.DOUQI_CAPABILTTY);
            if (douqiCap.isPresent()) {
                douqiCap.ifPresent((l) -> {
                            int i;
                            soul_property = l.getSoulProperty();
                            for (i = 0; i < DouPo.SOUL_MAX; ++i)
                                if (soul_property[i])
                                    soul_property_str += new TranslationTextComponent("gui." + Utils.MOD_ID + ".soul_" + i).getString();
                            soul_property = l.getFire();//被征用来获得异火数组
                            for (i = 0; i < DouPo.FIRE_MAX; ++i)
                                if (soul_property[i])
                                    ++fireNum;
                            body_str = l.getBodyName().getString();
                            eye_str = l.getEyeName().getString();
                        }
                );
            }
        }
    }

    @Override
    protected void init() {
//        this.minecraft.keyboardListener.enableRepeatEvents(true);
        super.init();
    }

    @Override
    public void render(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
        this.renderBackground(matrixStack);
        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);

        if (douqiCap.isPresent()) {
            douqiCap.ifPresent((l) -> {
                        int width = (this.minecraft.currentScreen.width - 630 / 2);
                        int height = (this.minecraft.currentScreen.height - 380 / 2);
                        this.minecraft.getTextureManager().bindTexture(PlayerRattributeGuiBgImg);
                        blit(matrixStack, width / 2, height / 2, 0, 0, 630 / 2, 380 / 2, 630 / 2, 380 / 2);

                        int end_num = 0;
                        double first_num = l.getDouQi(1);
                        if (first_num > 100000000) {
                            first_num /= 100000000;
                            end_num = 2;
                        } else if (first_num > 10000) {
                            first_num /= 10000;
                            end_num = 1;
                        }
                        content = new TranslationTextComponent("gui." + Utils.MOD_ID + ".douqi", String.format("%.2f",first_num));
                        drawString(matrixStack, this.minecraft.fontRenderer, content.getString() + (end_num == 1 ? "万" : end_num == 2 ? "亿" : ""), (width + 110) / 2, (height + 130) / 2, 0x000000);
                        end_num = 0;
                        first_num = l.getDouQiHaveMaxLevelUp();
                        if (first_num > 100000000) {
                            first_num /= 100000000;
                            end_num = 2;
                        } else if (first_num > 10000) {
                            first_num /= 10000;
                            end_num = 1;
                        }
                        content = new TranslationTextComponent("gui." + Utils.MOD_ID + ".douqi_show_max", String.format("%.2f",first_num));
                        drawString(matrixStack, this.minecraft.fontRenderer, content.getString() + (end_num == 1 ? "万" : end_num == 2 ? "亿" : ""), (width + 110) / 2, (height + 160) / 2, 0x000000);
                        content = new TranslationTextComponent("gui." + Utils.MOD_ID + ".douqi_level_all", l.getLevelAll()[3]);
                        drawString(matrixStack, this.minecraft.fontRenderer, content, (width + 110) / 2, (height + 190) / 2, 0x000000);
                        content = new TranslationTextComponent("gui." + Utils.MOD_ID + ".douqi_width_all", String.format("%.2f",l.getCompressAll()));
                        drawString(matrixStack, this.minecraft.fontRenderer, content, (width + 110) / 2, (height + 220) / 2, 0x000000);
                        content = new TranslationTextComponent("gui." + Utils.MOD_ID + ".grow_speed", String.format("%.2f",l.getDouQiAttraction()));
                        drawString(matrixStack, this.minecraft.fontRenderer, content, (width + 110) / 2, (height + 250) / 2, 0x000000);
                        content = new TranslationTextComponent("gui." + Utils.MOD_ID + ".attack", String.format("%.2f",l.getDamage()));
                        drawString(matrixStack, this.minecraft.fontRenderer, content, (width + 110) / 2, (height + 280) / 2, 0x000000);
                        content = new TranslationTextComponent("gui." + Utils.MOD_ID + ".defense", String.format("%.2f",l.getDefense()));
                        drawString(matrixStack, this.minecraft.fontRenderer, content, (width + 110) / 2, (height + 310) / 2, 0x000000);

                        content = new TranslationTextComponent("gui." + Utils.MOD_ID + ".soul_property", soul_property_str);
                        drawString(matrixStack, this.minecraft.fontRenderer, content, (width + 380) / 2, (height + 130) / 2, 0x000000);
                        content = new TranslationTextComponent("gui." + Utils.MOD_ID + ".soul", String.format("%.2f",l.getSoul()));
                        drawString(matrixStack, this.minecraft.fontRenderer, content, (width + 380) / 2, (height + 160) / 2, 0x000000);
                        content = new TranslationTextComponent("gui." + Utils.MOD_ID + ".soul_max", String.format("%.2f",l.getSoulMax()));
                        drawString(matrixStack, this.minecraft.fontRenderer, content, (width + 380) / 2, (height + 190) / 2, 0x000000);
                        content = new TranslationTextComponent("gui." + Utils.MOD_ID + ".fire", fireNum);
                        drawString(matrixStack, this.minecraft.fontRenderer, content, (width + 380) / 2, (height + 220) / 2, 0x000000);
                        content = new TranslationTextComponent("gui." + Utils.MOD_ID + ".body", body_str);
                        drawString(matrixStack, this.minecraft.fontRenderer, content, (width + 380) / 2, (height + 250) / 2, 0x000000);
                        content = new TranslationTextComponent("gui." + Utils.MOD_ID + ".eye", eye_str);
                        drawString(matrixStack, this.minecraft.fontRenderer, content, (width + 380) / 2, (height + 280) / 2, 0x000000);
                        content = new TranslationTextComponent("gui." + Utils.MOD_ID + ".douqi_put_percentage_max", (int) (l.getExDouQiReleasePercentage() * 100));
                        drawString(matrixStack, this.minecraft.fontRenderer, content, (width + 380) / 2, (height + 310) / 2, 0x000000);

                    }
            );

        }
        super.render(matrixStack, mouseX, mouseY, partialTicks);
    }
}

